package com.gexiaobao.pigeon.ui.fragment.mine.device

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Observer
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.AddDeviceParam
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentAddDeviceBinding
import com.gexiaobao.pigeon.permissions.PermissionInterceptor
import com.gexiaobao.pigeon.viewmodel.MyDeviceViewModel
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.common.Constant
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 17:38
 * @Describe    : 添加设备
 */
class FragmentAddDevice : BaseFragment<MyDeviceViewModel, FragmentAddDeviceBinding>() {

    private val REQUEST_CODE_SCAN_ERWEIMA = 11022
    private var title: String = ""
    private var mId: Int = 0

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        title = arguments?.getString("title").toString()
        if (title == "修改设备") {
            mId = arguments?.getInt("id")!!
            val imsi = arguments?.getString("imsi")
            mViewModel.deviceCode.set(imsi)
        }
        title.let {
            mDatabind.includeBar.toolbar.initClose(it) {
                nav().navigateUp()
            }
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.ivScanDeviceCode, mDatabind.tvAddDeviceSubmit) {
            when (it) {
                mDatabind.ivScanDeviceCode -> {
                    permissionRequest()
                }

                mDatabind.tvAddDeviceSubmit -> {
                    createPigeonDevice()
                }
            }
        }
    }

    private fun createPigeonDevice() {
        if (mViewModel.deviceCode.get().length < 15) {
            showMessage("设备号格式不正确")
            return
        }
        if (mViewModel.deviceCode.get().substring(0, 4) != "4600") {
            RxToast.showToast("设备号必须4600开头")
            return
        }
        if (title == "修改设备") {
            val params = AddDeviceParam(id = mId, imsi = mViewModel.deviceCode.get())
            mViewModel.updatePigeonDevice(params)
        } else {
            val params = AddDeviceParam(imsi = mViewModel.deviceCode.get())
            mViewModel.createPigeonDevice(params)
        }
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.addPigeonDevice.observe(viewLifecycleOwner, Observer {
            if (it.isSuccess) {
                RxToast.showToast("成功")
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        })

        mViewModel.upDatePigeonDevice.observe(viewLifecycleOwner, Observer {
            if (it.isSuccess) {
                RxToast.showToast("修改成功")
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        })
    }

    @Deprecated("Deprecated in Java")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (requestCode === REQUEST_CODE_SCAN_ERWEIMA && resultCode === Activity.RESULT_OK) {
            val content = data!!.getStringExtra(Constant.CODED_CONTENT)
            mViewModel.deviceCode.set(content)
        }
    }

    private fun permissionRequest() {
        XXPermissions.with(this)
            .permission(Permission.CAMERA)
            .interceptor(PermissionInterceptor())
            .request { _, allGranted ->
                if (allGranted) {
                    val intent = Intent(activity, CaptureActivity::class.java)
                    startActivityForResult(intent, REQUEST_CODE_SCAN_ERWEIMA)
                } else {
                    nav().navigateUp()
                }
            }
    }
}